package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.lsn.SendMsgCenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatChatAddZhuanZhangActivity extends BaseActivity {
    @BindView(R2.id.tv_sender_name)
    TextView       tvSenderName;
    @BindView(R2.id.iv_avatar)
    ImageView      ivAvatar;
    @BindView(R2.id.iv_arrow)
    ImageView      ivArrow;
    @BindView(R2.id.btn_pick_sender)
    RelativeLayout btnPickSender;
    @BindView(R2.id.rv)
    RecyclerView   rv;
    @BindView(R2.id.et_money)
    EditText       etMoney;
    @BindView(R2.id.et_remark)
    EditText       etRemark;
    @BindView(R2.id.btn_save)
    Button         btnSave;
    @BindView(R2.id.tv_tips)
    TextView       tvTips;
    private WechatContactBean       contactBean;
    private boolean                 isRoomChat;
    private ContactAdapter          adapter;
    private List<WechatContactBean> list;

    public WechatChatAddZhuanZhangActivity() {
        super(R.layout.wechat_activity_chat_send_zhuanzhang);
    }

    public static void start(Activity act, WechatContactBean contactBean) {
        Intent it = new Intent(act, WechatChatAddZhuanZhangActivity.class);
        it.putExtra("isRoomChat", false);
        it.putExtra("WechatContactBean", contactBean);
        act.startActivity(it);
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        //添加数据（me）
        WechatContactBean me = WechatContactBean.instanceNewContactMe(this);
        onSenderChange(me);
        list.add(me);
        //添加数据（other）
        isRoomChat = getIntent().getBooleanExtra("isRoomChat", false);
        if (!isRoomChat) {
            contactBean = (WechatContactBean) getIntent().getSerializableExtra("WechatContactBean");
            list.add(contactBean);
        }
        //发送者选择adapter
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        adapter = new ContactAdapter(R.layout.wechat_select_sender_contact_item, list);
        rv.setAdapter(adapter);
        //金额监听
        etMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    double m = Double.parseDouble(s.toString());
                    if (m > 200000) {
                        tvTips.setVisibility(View.VISIBLE);
                    } else {
                        tvTips.setVisibility(View.GONE);
                    }
                    if (m > 0 && m <= 200000) {
                        btnSave.setEnabled(true);
                    } else {
                        btnSave.setEnabled(false);
                    }
                } catch (Exception e) {
                    ToastUtils.showShort("金额输入有误");
                    btnSave.setEnabled(false);
                }
            }
        });
        setTypeface(etMoney);
    }

    @OnClick({R2.id.btn_pick_sender, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_pick_sender) {
            if (rv.getVisibility() == View.VISIBLE) {
                rv.setVisibility(View.GONE);
            } else {
                rv.setVisibility(View.VISIBLE);
            }
        } else if (i == R.id.btn_save) {
            String moneyStr = etMoney.getText().toString();
            if (TextUtils.isEmpty(moneyStr)){
                ToastUtils.showShort("金额不能为空");
                return;
            }
            try {
                Double.parseDouble(moneyStr);
            } catch (Exception e) {
                ToastUtils.showShort("金额输入有误");
                e.printStackTrace();
                return ;
            }
            String remark = etRemark.getText().toString();
            if (TextUtils.isEmpty(remark)) {
                remark = (sender.getId() == -1 ? "转账给" + contactBean.getName() : "转账给你");
            }
            SendMsgCenter.sendZhuanZhang(new SendMsgCenter.SendMsgListener() {
                @Override
                public void onSendSuc() {
                    finish();
                    KeyboardUtils.hideSoftInput(WechatChatAddZhuanZhangActivity.this);
                }

                @Override
                public void onSendError(String errorMsg) {
                }
            }, sender.getId(), contactBean.getId(), sender.getId(), Double.parseDouble(moneyStr), remark);
        }
    }

    private WechatContactBean sender;

    public class ContactAdapter extends BaseQuickAdapter<WechatContactBean, BaseViewHolder> {
        public ContactAdapter(int layoutResId, List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final WechatContactBean vo) {
            helper.setText(R.id.name, vo.getName());
            WechatContactBean.setupAvatarIntoImageView(vo,
                    (ImageView) helper.getView(R.id.iv_avatar), 12);
            helper.setChecked(R.id.rb, vo.isSelected());
            helper.getView(R.id.vg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (WechatContactBean item : ContactAdapter.this.getData()) {
                        item.setSelected(false);
                    }
                    notifyDataSetChanged();
                    onSenderChange(vo);
                }
            });
        }
    }

    private void onSenderChange(WechatContactBean vo) {
        vo.setSelected(true);
        sender = vo;
        rv.setVisibility(View.GONE);
        WechatContactBean.setupAvatarIntoImageView(vo, ivAvatar, 12);
        tvSenderName.setText(vo.getName());
    }

}
